﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;

namespace PlatypusPlanner.Components.Utilities
{
    public class QueryStringUtility
    {
        #region singleton

        private static readonly QueryStringUtility _instance = new QueryStringUtility();
        private QueryStringUtility() { }
        public static QueryStringUtility Instance() { return _instance; }

        #endregion

        #region methods

        public string GetQueryStringValue(string queryStringId)
        {
            if (string.IsNullOrEmpty(queryStringId))
                throw new Exception("You must pass a query string variable to check for with this function.");

            if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[queryStringId]))
                return HttpContext.Current.Request.QueryString[queryStringId];

            return string.Empty;
        }
        public Int32 GetQueryStringValueInt32(string queryStringId)
        {
            if (string.IsNullOrEmpty(queryStringId))
                throw new Exception("You must pass a query string variable to check for with this function.");

            string queryValue = HttpContext.Current.Request.QueryString[queryStringId];

            if (!string.IsNullOrEmpty(queryValue))
                if (Regex.IsMatch(queryValue, "^[0-9-]*$"))
                    return Convert.ToInt32(queryValue);

            return 0;
        }

        public Int64 GetQueryStringValueInt64(string queryStringId)
        {
            if (string.IsNullOrEmpty(queryStringId))
                throw new Exception("You must pass a query string variable to check for with this function.");

            string queryValue = HttpContext.Current.Request.QueryString[queryStringId];

            if (!string.IsNullOrEmpty(queryValue))
                if (Regex.IsMatch(queryValue, "^[0-9]*$"))
                    return Convert.ToInt32(queryValue);

            return 0;
        }

        #endregion

        #region url redirects

        public void RedirectToPageOrReturnUrl(string page)
        {
            if (string.IsNullOrEmpty(QueryStringUtility.Instance().GetQueryStringValue(Globals.RETURN_URL_KEY)))
                HttpContext.Current.Response.Redirect(page);

            HttpContext.Current.Response.Redirect(QueryStringUtility.Instance().GetQueryStringValue(Globals.RETURN_URL_KEY));
        }
        public void RedirectToMessage(int messageId)
        {
            HttpContext.Current.Response.Redirect(string.Format("/Message.aspx?message={0}", messageId));
        }
        public void RedirectWithMessage(string page, int messageId)
        {
            string lowered = page.ToLower();
            string tab = string.Empty;

            if (lowered.Contains("message="))
                page = page.Substring(0, page.IndexOf("?"));

            if (lowered.Substring(lowered.Length - 1, 1) == "?")
                page = page.Substring(0, lowered.Length - 1);

            if (page.Contains("?"))
                HttpContext.Current.Response.Redirect(string.Format("{0}{2}&message={1}", page, messageId, tab));
            else
                HttpContext.Current.Response.Redirect(string.Format("{0}{2}?message={1}", page, messageId, tab));
        }
        public void RedirectToCurrentPage()
        {
            HttpContext.Current.Response.Redirect(HttpContext.Current.Request.RawUrl.ToString());
        }
        public string GenerateMessagePagePath(string page, int messageId)
        {
            string lowered = page.ToLower();
            string tab = string.Empty;

            if (lowered.Contains("message="))
                page = page.Substring(0, page.IndexOf("?"));

            if (lowered.Substring(lowered.Length - 1, 1) == "?")
                page = page.Substring(0, lowered.Length - 1);

            if (page.Contains("?"))
                return string.Format("{0}{2}&message={1}", page, messageId, tab);
            else
                return string.Format("{0}{2}?message={1}", page, messageId, tab);
        }
        #endregion

        #region cookie functions

        public string GetCookie(HttpContext context, string name)
        {
            HttpCookie cookie = context.Request.Cookies.Get(name);
            if (cookie != null)
                return cookie.Value;
            return string.Empty;
        }
        public void AddCookie(HttpContext context, string name, string value, DateTime expirationDate, string domain = "")
        {
            HttpCookie cookie = new HttpCookie(name);
            cookie.Value = value;
            cookie.Expires = expirationDate;
            if (!string.IsNullOrEmpty(domain))
                cookie.Domain = domain;
            context.Response.Cookies.Add(cookie);
        }
        public void DeleteCookie(HttpContext context, string name)
        {
            if (context.Request.Cookies[name] != null)
            {
                HttpCookie cookie = new HttpCookie(name);
                cookie.Expires = DateTime.Now.AddDays(-1d);
                context.Response.Cookies.Add(cookie);
            }
        }

        #endregion
    }
}
